#include <thread>
#include <vector>
#include <atomic>
#include <chrono>
#include <iostream>
#include "blocking_queue.h"

int main()
{
    const int num_producers = 8;
    const int num_consumers = 4;
    const int items_per_producer = 10000;
    std::atomic<int> total_produced{0};
    std::atomic<int> total_consumed{0};

    auto queue = BlockingQueue<int>::create(1024);
    std::vector<std::thread> producers;
    std::vector<std::thread> consumers;
    for (int i = 0; i < num_producers; ++i) {
        producers.emplace_back([i, queue, items_per_producer, &total_produced]() {
            for (int j = 0; j < items_per_producer; ++j) {
                if (queue->push(i * items_per_producer + j, 10)) {
                    total_produced ++;
                }
            }
        });
    }
    for (int i = 0; i < num_consumers; ++i) {
        consumers.emplace_back([queue, &total_consumed]() {
            int item;
            while (queue->pop(item, 10)) {
                total_consumed ++;
            }
        });
    }
    for (auto& t : producers) {
        t.join();
    }
    for (auto& t : consumers) {
        t.join();
    }
    std::cout << "total_produced: " << total_produced << std::endl;
    std::cout << "total_consumed: " << total_consumed << std::endl;
    return 0;
}